package com.apollow.oschina.ui;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.apollow.oschina.R;
import com.apollow.oschina.base.BaseActivity;
import com.apollow.oschina.bean.DetailTweetBean;
import com.apollow.oschina.protocol.TweetDetailProtocol;
import com.apollow.oschina.ui.empty.MainTab;
import com.apollow.oschina.view.TakePhotoPopWin;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

import static com.apollow.oschina.R.id.drawerLayout;


public class MainActivity extends BaseActivity {
    public List<Fragment> fragmnets = new ArrayList<>();
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(drawerLayout)
    DrawerLayout mDrawer;
    @BindView(R.id.main_fragment_container)
    FrameLayout mlinearlayout;
    @BindView(R.id.ll_mennu_git)
    LinearLayout mMenugit;
    @BindView(R.id.ll_menu_quest)
    LinearLayout mMenuquest;
    @BindView(R.id.ll_menu_blog)
    LinearLayout mMenublog;
    @BindView(R.id.ll_menu_opensoft)
    LinearLayout mMenuopensoft;
    @BindView(R.id.ll_menu_setting)
    LinearLayout mMenusetting;
    @BindView(R.id.ll_menu_exit)
    LinearLayout mMenuexit;
    @BindView(R.id.tabhost)
    FragmentTabHost mTabHost;
    @BindView(R.id.iv_quick_option)
    ImageView mIvQuickOption;
    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.nv)
    LinearLayout mNv;
    private ActionBarDrawerToggle mToggle;
    private TakePhotoPopWin mTakePhotoPopWin;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initToolbar();
        initView();
        initEvnet();
    }
    private void initView() {
        mTabHost.setup(this, getSupportFragmentManager(), R.id.main_fragment_container);
        if (Build.VERSION.SDK_INT > 10) { //大于10版本，不显示分割线
            TabWidget tabWidget = mTabHost.getTabWidget();
            tabWidget.setShowDividers(0);
        }
        addTabs();
    }

    private void addTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabHost.TabSpec tab = mTabHost.newTabSpec(this.getResources().getString(mainTab.getResName()));

            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {
                indicator.setVisibility(View.INVISIBLE);
            }
            title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            // ------------------------------------------------- 以上 ↑

            Bundle bundle = new Bundle();
            bundle.putString("key",
                    "content: " + getString(mainTab.getResName()));
            mTabHost.addTab(tab, mainTab.getClz(), bundle);
        }
    }
    private void initToolbar() {
        mToolbar.setNavigationIcon(R.drawable.ic_menu_back);
        setSupportActionBar(mToolbar);
        mToolbar.inflateMenu(R.menu.tool_menu);
        mDrawer = (DrawerLayout) findViewById(drawerLayout);
        setSupportActionBar(mToolbar);//与Activity关联;
        mToggle = new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close);
        mToggle.syncState();//actionBar与DrawerLayout关联;
    }

    private void initEvnet() {
        // TODO: 2017/1/13 toolbar搜索菜单事件监听;
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.title_search:
                        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
                        new Thread(new Runnable() {
                       TweetDetailProtocol mProtocol=new TweetDetailProtocol();
                            @Override
                            public void run() {
                                DetailTweetBean data = mProtocol.getData();
                                System.out.println(data.getTweet().getBody());
                            }
                        }).start();
                        break;
                }
                return false;
            }
        });

        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                mToggle.onDrawerSlide(drawerView, slideOffset);
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                mToggle.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                mToggle.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                mToggle.onDrawerStateChanged(newState);
            }
        });
    }

    @OnLongClick(R.id.iv_quick_option)
    public boolean onLongClick(View v) {
        Intent intent = new Intent(MainActivity.this, AddressActivity.class);
        startActivity(intent);
        return false;
    }

    // TODO: 2017/1/14 底部RadioGroup与fragemnts关联,drawlayout左边布局监听;
    @OnClick({ R.id.iv_quick_option, R.id.ll_mennu_git,
            R.id.ll_menu_exit, R.id.ll_menu_quest, R.id.ll_menu_setting, R.id.ll_menu_opensoft, R.id.ll_menu_blog})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_menu_quest:
                Toast.makeText(MainActivity.this, "菜单项技术问答", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_menu_opensoft:
                Toast.makeText(MainActivity.this, "菜单项开源软件", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_menu_blog:
                Toast.makeText(MainActivity.this, "菜单项被点了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_mennu_git:
                Toast.makeText(MainActivity.this, "菜单项Git被点", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_menu_setting:
                Toast.makeText(MainActivity.this, "菜单项设置", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_menu_exit:
                int pid = Process.myPid();
                Process.killProcess(pid);
                break;
            case R.id.iv_quick_option:
                mTakePhotoPopWin = new TakePhotoPopWin(this, onClickListener);
                mTakePhotoPopWin.showAtLocation(findViewById(R.id.tabhost), Gravity.CENTER, 0, 0);
                break;
        }

    }

    // TODO: 2017/1/14 菜单项布局填充
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tool_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                toggle();
        }
        return super.onOptionsItemSelected(item);
    }

    // TODO: 2017/1/14 toolbar右上角的点击事件
    public void toggle() {
        if (mDrawer.isDrawerOpen(Gravity.LEFT)) {
            mDrawer.closeDrawer(Gravity.LEFT);
        } else {
            mDrawer.openDrawer(Gravity.LEFT);
        }
    }

    // TODO: 2017/1/14 底部popuwindow接口点击事件功能扩展;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_take_photo:
                    Toast.makeText(MainActivity.this, "照相", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.ll_take_note:
                    Toast.makeText(MainActivity.this, "笔记", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.ll_take_album:
                    Toast.makeText(MainActivity.this, "相册", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.im_bottom_back:
                    mTakePhotoPopWin.dismiss();
                    break;
            }
        }
    };

}
